/**
 * 
 */
package com.vanstone.imgapp.img.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.vanstone.common.ObjectDuplicateException;
import com.vanstone.common.debug.MyAssert;
import com.vanstone.imgapp.api.img.Tag;
import com.vanstone.imgapp.api.img.services.TagService;
import com.vanstone.imgapp.common.id.SystemIDSchema;
import com.vanstone.imgapp.common.id.services.IDService;
import com.vanstone.imgapp.img.dal.object.CmsTagDO;

/**
 * @author shipeng <br />
 * 
 */
@Service("tagService")
public class TagServiceImpl implements TagService {

  @Autowired
  private MongoTemplate mongoTemplate;

  @Autowired
  private IDService idService;

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#addTag(java.lang.String)
   */
  @Override
  public Tag addTag(String tagname) throws ObjectDuplicateException {
    MyAssert.hasText(tagname);
    Query query = Query.query(Criteria.where("tagname").is(tagname));
    CmsTagDO cmsTagDO = this.mongoTemplate.findOne(query, CmsTagDO.class);
    if (cmsTagDO != null) {
      throw new ObjectDuplicateException();
    }
    Tag tag = new Tag();
    tag.setId(idService.nextID(SystemIDSchema.Cms_Tag));
    this.mongoTemplate.insert(BeanUtil.toCmsTagDO(tag));
    return tag;
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#saveorget(java.lang.String)
   */
  @Override
  public Tag saveorget(String tagname) {
    MyAssert.hasText(tagname);
    Tag tag = this.getTagByTagname(tagname);
    if (tag != null) {
      return tag;
    }
    tag = new Tag();
    tag.setId(idService.nextID(SystemIDSchema.Cms_Tag));
    tag.setTagname(tagname);
    this.mongoTemplate.insert(BeanUtil.toCmsTagDO(tag));
    return tag;
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTag(long)
   */
  @Override
  public Tag getTag(long id) {
    CmsTagDO cmsTagDO = this.mongoTemplate.findById(id, CmsTagDO.class);
    if (cmsTagDO == null) {
      return null;
    }
    return BeanUtil.toTag(cmsTagDO);
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTagByTagname(java.lang.String)
   */
  @Override
  public Tag getTagByTagname(String tagname) {
    MyAssert.hasText(tagname);
    Query query = Query.query(Criteria.where("tagname").is(tagname));
    CmsTagDO cmsTagDO = this.mongoTemplate.findOne(query, CmsTagDO.class);
    if (cmsTagDO == null) {
      return null;
    }
    return BeanUtil.toTag(cmsTagDO);
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTags(java.lang.String, long, long)
   */
  @Override
  public List<Tag> getTags(String key, long offset, long limit) {
    Query query = new Query();
    query.with(new Sort(Direction.DESC, "relcount"));
    query.skip((int) offset);
    query.limit((int) limit);
    List<CmsTagDO> cmsTagDOs = this.mongoTemplate.find(query, CmsTagDO.class);
    if (cmsTagDOs == null || cmsTagDOs.size() <= 0) {
      return null;
    }
    List<Tag> tags = new ArrayList<>();
    for (CmsTagDO cmsTagDO : cmsTagDOs) {
      Tag tag = BeanUtil.toTag(cmsTagDO);
      tags.add(tag);
    }
    return tags;
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTotalTags(java.lang.String)
   */
  @Override
  public long getTotalTags(String key) {
    Query query = new Query();
    return this.mongoTemplate.count(query, CmsTagDO.class);
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTagsAsMap(java.util.List)
   */
  @Override
  public Map<Long, Tag> getTagsAsMap(List<Long> ids) {
    Criteria criteria = new Criteria();
    criteria.in(ids);
    Query query = Query.query(criteria);
    List<CmsTagDO> cmsTagDOs = this.mongoTemplate.find(query, CmsTagDO.class);
    if (cmsTagDOs == null || cmsTagDOs.size() <= 0) {
      return null;
    }
    Map<Long, Tag> map = new HashMap<>();
    for (CmsTagDO cmsTagDO : cmsTagDOs) {
      Tag tag = BeanUtil.toTag(cmsTagDO);
      map.put(tag.getId(), tag);
    }
    return map;
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTags(java.util.List)
   */
  @Override
  public List<Tag> getTags(List<Long> ids) {
    Criteria criteria = Criteria.where("_id").in(ids);
    Query query = Query.query(criteria);
    List<CmsTagDO> cmsTagDOs = this.mongoTemplate.find(query, CmsTagDO.class);
    if (cmsTagDOs == null || cmsTagDOs.size() <= 0) {
      return null;
    }
    List<Tag> list = new ArrayList<>();
    for (CmsTagDO cmsTagDO : cmsTagDOs) {
      Tag tag = BeanUtil.toTag(cmsTagDO);
      list.add(tag);
    }
    return list;
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTagsByCategoryID(java.lang.String,
   * long, long)
   */
  @Override
  public List<Tag> getTagsByCategoryID(String categoryID, long offset, long limit) {
    // TODO Auto-generated method stub
    return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.vanstone.imgapp.api.img.services.TagService#getTotalTagsByCategoryID(java.lang.String)
   */
  @Override
  public List<Tag> getTotalTagsByCategoryID(String categoryID) {
    // TODO Auto-generated method stub
    return null;
  }

}
